package com.cn.jdia.mq.mode;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.cn.jdia.mq.db.DBManager4Mysql;
import com.cn.jdia.mq.utils.Constants;

public class XmlMode {

	public static void parse(Map<String, String> config) {
		System.out.println(config.get("mode") + " " + config.get("mqcontent"));
		DBManager4Mysql db = DBManager4Mysql.createInstance();
		db.connectDB();

		if (config.get("mqcontent").equals(Constants.CONTENT_TOPIC)) {
			getTopic(config, db);
		} else if (config.get("mqcontent").equals(Constants.CONTENT_QUEUE)) {
			getQueue(config, db);
		} else if (config.get("mqcontent").equals(Constants.CONTENT_QUEUE_TOPIC)) {
			getTopic(config, db);
			getQueue(config, db);
		}

		db.closeDB();
	}

	public static void getQueue(Map<String, String> config, DBManager4Mysql db) {
		System.out.println("=====Get Queue=====");
		// http://172.20.5.122:8161/admin/xml/queues.jsp
		String cmd = "curl http://" + config.get("ip") + ":" + config.get("port") + "/admin/xml/queues.jsp";
		System.out.println(cmd);
		String[] cmds = { "/bin/sh", "-c", cmd };

		try {
			Runtime run = Runtime.getRuntime();
			Process ps = run.exec(cmds);

			BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
			StringBuffer sb = new StringBuffer();
			String line;

			String xml = "";
			while ((line = br.readLine()) != null) {
				xml += line;
			}

			Document doc = DocumentHelper.parseText(xml);
			Element root = doc.getRootElement();
			System.out.println(root.getName());

			String name = null;
			int pending = 0;
			int consumer = 0;
			int enqueue = 0;
			int dequeue = 0;

			String insert = null;

			List<Element> children = root.elements();
			if (children.size() != 0) {
				String del = "delete from mqqueue where servername='" + config.get("severname") + "' and ip='"
						+ config.get("ip") + "'";
				System.out.println(del);
				int delret = db.executeUpdate(del);
				System.out.println("delret=" + delret);
			}

			for (Element ele : children) {
				Element cele = ele.element("stats");

				pending = Integer.parseInt(cele.attributeValue("size"));
				consumer = Integer.parseInt(cele.attributeValue("consumerCount"));
				enqueue = Integer.parseInt(cele.attributeValue("enqueueCount"));
				dequeue = Integer.parseInt(cele.attributeValue("dequeueCount"));

				// System.out.println(ele.attributeValue("name")+","+pending+","+consumer+","+enqueue
				// +","+dequeue);
				insert = "insert into mqqueue (servername,ip,name,consumer,enqueue,dequeue,pending) values ('"
						+ config.get("severname") + "','" + config.get("ip") + "','" + ele.attributeValue("name") + "',"
						+ consumer + "," + enqueue + "," + dequeue + "," + pending + ")";
				// System.out.println(insert);
				db.executeUpdate(insert);
			}
			System.out.println("=====Insert Queues End=====");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void getTopic(Map<String, String> config, DBManager4Mysql db) {
		System.out.println("=====Get Topic=====");
		String cmd = "curl http://" + config.get("ip") + ":" + config.get("port") + "/admin/xml/topics.jsp";
		System.out.println(cmd);
		String[] cmds = { "/bin/sh", "-c", cmd };

		try {
			Runtime run = Runtime.getRuntime();
			Process ps = run.exec(cmds);

			BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
			StringBuffer sb = new StringBuffer();
			String line;

			String xml = "";
			while ((line = br.readLine()) != null) {
				xml += line;
			}

			Document doc = DocumentHelper.parseText(xml);

			Element root = doc.getRootElement();
			System.out.println(root.getName());

			List<Element> children = root.elements();
			if (children.size() != 0) {
				String del = "delete from mqtopic where servername='" + config.get("severname") + "' and ip='"
						+ config.get("ip") + "'";
				System.out.println(del);
				int delret = db.executeUpdate(del);
				System.out.println("delret=" + delret);
			}

			int consumer = 0;
			int enqueue = 0;
			int dequeue = 0;

			String insert = null;

			for (Element ele : children) {
				// System.out.println(ele.attributeValue("name"));
				Element cele = ele.element("stats");

				consumer = Integer.parseInt(cele.attributeValue("consumerCount"));
				enqueue = Integer.parseInt(cele.attributeValue("enqueueCount"));
				dequeue = Integer.parseInt(cele.attributeValue("dequeueCount"));

				// System.out.println(consumer + "," + enqueue + "," + dequeue);
				insert = "insert into mqtopic (servername,ip,name,consumer,enqueue,dequeue) values ('"
						+ config.get("severname") + "','" + config.get("ip") + "','" + ele.attributeValue("name") + "',"
						+ consumer + "," + enqueue + "," + dequeue + ")";
				// System.out.println(insert);
				db.executeUpdate(insert);
			}

			System.out.println("=====Insert Topics End=====");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
